package dados;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import negocios.cinemaBeans.*;

public class RepositorioSala implements Serializable {
	private ArrayList<Sala> listaSala = new ArrayList<Sala>();
	
	private static RepositorioSala instanciaSala;
	
	private RepositorioSala(){
		
	}
	
	public static RepositorioSala getInstanciaSala(){
		instanciaSala = recuperaArquivoSala();
		if (instanciaSala == null){
			instanciaSala = new RepositorioSala();
		}
		return instanciaSala;
	}
	
	
	public ArrayList<Sala> getListaSala(){
		return this.listaSala;
	}
	
	public void addSala(Sala sala){
		this.listaSala.add(sala);
		gravaArquivoSala();
	
		
	}
	
	public void delSala(Sala sala){
		this.listaSala.remove(sala);
	}
	
	
	public static void gravaArquivoSala(){
		File arquivoSalas;
		FileOutputStream fos;
		ObjectOutputStream oos;
		
		try {
			arquivoSalas = new File("salas.dat");
			fos = new FileOutputStream(arquivoSalas);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(instanciaSala);
			oos.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected static RepositorioSala recuperaArquivoSala(){
		File arquivoSalas;
		FileInputStream fis;
		ObjectInputStream ois;
		RepositorioSala clone = null;
		
		try{
			arquivoSalas = new File("salas.dat");
			fis = new FileInputStream(arquivoSalas);
			ois = new ObjectInputStream(fis);
			clone = (RepositorioSala)ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			return clone;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return clone;
		
	}
	
}
